<?php
/**
 * Created by PhpStorm.
 * User: liuhong
 * Date: 2018/7/27
 * Time: 下午5:00
 */

namespace app\common\service;


class BaiduApiService extends BaseService
{

    /**
     * apiKey
     * @var string
     */
    protected $appId ;
    protected $apiKey;
    protected $secretKey;

    public function __construct()
    {
        vendor('baiduapi.AipFace');
        vendor('baiduapi.lib');
        $this->appId='11613254';
        $this->apiKey='XsGv3ESddbypqiLDkYzbrPNz';
        $this->secretKey='HriDWHhhNp78H3NlWOaO5TGe2G66zWlt';
    }

    //获取token认证
    public function auth($isTrue=false){
        $apiBase=new \AipBase($this->appId,$this->apiKey,$this->secretKey);
        $re= $apiBase->auth($isTrue);
        return $re['access_token'];
    }

    public function createUserGroup(){
        $aipFace=new \AipFace($this->appId,$this->apiKey,$this->secretKey);
        $re=$aipFace->groupAdd('1112');
        echo json_encode($re);die;
    }


    /**
     * 新建会议／修改会议接口
     * @param $id
     * @param $gId
     * @param $name
     * @param $desc
     * @param $startTime
     * @param null $endTime
     * @param null $headImage
     * @param null $token
     * @return mixed
     */
    public function addProject($id,$gId,$name,$startTime,$desc,$endTime=null,$headImage=null,$token=null){
        $auth=self::auth();
        $access_token=$auth['access_token'];
        $data['uProjectId']=$id;
        $data['name']=$name;
        $data['desc']=$desc;
        $data['startTime']=$startTime;
        $data['gId']=$gId;
        if(!empty($endTime)){
            $data['endTime']=$endTime;
        }if(!empty($headImage)){
            $data['headImage']=$headImage;
        }
        if(!empty($token)){
            $data['token']=$token;
        }
        $data['signinSuccessTip']='签到成功';
        $data['signinFailTip']='签到失败';
        $data['interactType']=1;
        $data['feedBackUrl']=request()->host().'/index/Thirdpaid/feedBack';//平台推送地址
        $url='https://aip.baidubce.com/api/v1/solution/direct/meeting/project?access_token='.$access_token;
        $re=CurlService::S()->https_request($url,$data,'json');
        return $re;
    }

    /**
     *  查询会议接口
     * @param $token
     * @param int $source
     * @return mixed
     */
    public function queryInfo($token,$source=0){
        $access_token=self::auth();
        $url='https://aip.baidubce.com/api/v1/solution/direct/meeting/project/info?access_token='.$access_token;
        $data['token']=$token;
        $data['source']=$source;
        return json_decode(CurlService::S()->httpMethodPost($url,$data,true));
    }


    /**
     * 人脸验证
     * @param $data
     * @param int $source
     * @return mixed
     */
    public function verifyFace($data,$source=0){
        $access_token=BaiduApiService::S()->auth();
        $url='https://aip.baidubce.com/api/v1/solution/direct/meeting/verify?access_token='.$access_token;
        $data['source']=$source;
        $re=json_decode(CurlService::S()->httpMethodPost($url,$data,true),true);
        LogInfoService::S()->writeLog('BaiduApi','verifyFace',$re);//记录验证结果
        return $re;
    }

    /**
     * 判断是否是人脸
     * @param $img
     * @param string $imgType
     * @return bool
     */
    public function isFace($img,$imgType='BASE64'){
        $aipFace=new \AipFace($this->appId,$this->apiKey,$this->secretKey);
        $re=$aipFace->detect($img,$imgType);
        if($re['result']['face_list'][0]['face_probability']>0.9){ //一半的概率是人脸
            return true;
        }else{
            return false;
        }
    }

    //人脸报名接口
    public function faceApply($uid,$name,$token,$telephone,$faceImage,$data=array()){
        $access_token=BaiduApiService::S()->auth(true);
        $url='https://aip.baidubce.com/api/v1/solution/direct/meeting/apply?access_token='.$access_token;
        $data['uUserId']=$uid;
        $data['name']=$name;
        $data['token']=$token;
        $data['telephone']=$telephone;
        $data['faceImage']=$faceImage;
        if(!empty($data)){
            $data['data']=json_encode($data);
        }
        $re=CurlService::S()->https_request($url,$data,'json');
        return json_decode($re,true);
    }






}